package ru.ifmo.mlarinsky.classificator.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import ru.ifmo.mlarinsky.classificator.io.ClassificatorRuntimeException;

/**
 * @author Mikhail Larinskiy
 */
public class QuoteDate extends Date {
    private static final String FORMAT_PATTERN = "MM/dd/yyyy";
    private final Calendar calendar = Calendar.getInstance();

    private final Date date;

    public QuoteDate(Date date) {
        this.date = date;
        calendar.setTime(date);
    }

    @Override
    public Object clone() {
        return date.clone();
    }

    @Override
    public int getYear() {
        return calendar.get(Calendar.YEAR);
    }

    @Override
    public int getMonth() {
        return calendar.get(Calendar.MONTH);
    }

    @Override
    public int getDay() {
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public long getTime() {
        return date.getTime();
    }

    @Override
    public void setTime(long time) {
        date.setTime(time);
    }

    @Override
    public boolean before(Date when) {
        return date.before(when);
    }

    @Override
    public boolean after(Date when) {
        return date.after(when);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof QuoteDate)
            return date.equals(((QuoteDate) obj).date);

        return date.equals(obj);
    }

    @Override
    public int compareTo(Date anotherDate) {
        if (anotherDate instanceof QuoteDate)
            return date.compareTo(((QuoteDate) anotherDate).date);
        return date.compareTo(anotherDate);
    }

    @Override
    public int hashCode() {
        return date.hashCode();
    }

    @Override
    public String toString() {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_PATTERN);
        return format.format(date);
    }

    /**
     * Creates QuoteDate from string
     * @param string string value of date. Must follw pattern MM/dd/yyyy
     * @return quote date built from string value
     */
    public static QuoteDate valueOf(String string) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_PATTERN);
        try {
            return new QuoteDate(format.parse(string));
        } catch (ParseException e) {
            throw new ClassificatorRuntimeException("Failed to buid date from string " + string, e);
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////// Testing /////////////////////////////////////////////////

    public static void main(String[] args) {
        testValueOf();
    }

    private static void testValueOf() {
        String dateString = "04/04/2011";

        System.out.println("Date = " + valueOf(dateString).date.toString());
    }
}
